import userapi from '@/api/declaration/userApi'
import httpRequest from '@/config/axios/httpRequest'

// 获得授权信息
export const getAuthorize = async (clientId: string) => {
  return await httpRequest.get(userapi.Authorize2Login, { params: { clientId } })
}

// 发起授权
export const authorize = async (
  responseType: string,
  clientId: string,
  redirectUri: string,
  state: string,
  autoApprove: boolean,
  checkedScopes: string[],
  uncheckedScopes: string[]
) => {
  // 构建 scopes
  const scopes = {}
  for (const scope of checkedScopes) {
    scopes[scope] = true
  }
  for (const scope of uncheckedScopes) {
    scopes[scope] = false
  }
  // 发起请求
  return await httpRequest.post(
    userapi.Authorize2Login,
    { responseType, clientId, redirectUri, state, autoApprove, checkedScopes, uncheckedScopes },
    { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
  )
}
